/* File:        	RequestAuthorizationImpl.java
* Package:			org.esi.authz.pdp.request
* Created:      	29-abr-2011
* Last Changed: 	0:10:48
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.common.request.component;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;

import org.jboss.security.xacml.core.model.context.ActionType;
import org.jboss.security.xacml.core.model.context.EnvironmentType;
import org.jboss.security.xacml.core.model.context.RequestType;
import org.jboss.security.xacml.core.model.context.ResourceType;
import org.jboss.security.xacml.core.model.context.SubjectType;
import org.jboss.security.xacml.factories.RequestAttributeFactory;
import org.jboss.security.xacml.interfaces.XACMLConstants;
import org.jboss.security.xacml.sunxacml.ctx.RequestCtx;


// TODO: Auto-generated Javadoc
/**
 * The Class RequestAuthorizationImpl.
 */
public class RequestAuthorizationImpl implements IRequestAuthorization 
{

	//Create a Request Type
	/** The request type. */
	private RequestType requestType = new RequestType();

    /* (non-Javadoc)
     * @see org.esi.authz.pdp.request.model.IRequestAuthorization#RequestAuthorization()
     */
    @Override
	public void RequestAuthorization() {
        //Create an Environment Type (Optional)
    	EnvironmentType environmentType = new EnvironmentType();
		
		environmentType.getAttribute().add(
				RequestAttributeFactory.createDateTimeAttributeType(XACMLConstants.ATTRIBUTEID_CURRENT_TIME, null));
		requestType.setEnvironment(environmentType);

	}

    /* (non-Javadoc)
     * @see org.esi.authz.pdp.request.model.IRequestAuthorization#RequestAuthorization(java.util.HashMap)
     */
    @Override
	public void RequestAuthorization(HashMap<EnvAttrTypeImpl, String> envData) {
        //Create an Environment Type (Optional)
    	EnvironmentType environmentType = new EnvironmentType();
		environmentType.getAttribute().add(
				RequestAttributeFactory.createDateTimeAttributeType(XACMLConstants.ATTRIBUTEID_CURRENT_TIME, null));		
		requestType.setEnvironment(environmentType);

	}


    
	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#setSubject(java.util.HashMap, java.util.HashMap)
	 */
	@Override
	public void setSubject(HashMap<SubjectAttrTypeImpl, String> subjectData,
			HashMap< SubjectAttrTypeImpl, String> groupData) {
	
	    //Create a Subject Type
		Iterator<SubjectAttrTypeImpl> itS = subjectData.keySet().iterator();
	    //Create a Subject Group Type
		//Iterator<SubjectAttrTypeImpl> itG = groupData.keySet().iterator(); 
		while(itS.hasNext()) 
		{ 
			SubjectAttrTypeImpl keyS = itS.next(); 
			String valS = subjectData.get(keyS); 
			//Create a subject type
		    SubjectType subject = new SubjectType();
		  	subject.getAttribute().add(
		  			RequestAttributeFactory.createStringAttributeType(
		  					keyS.attribute, "tecnalia.com", valS));
		  	
			//Associate to Group 
		  	/*SubjectAttrTypeImpl keyG = (SubjectAttrTypeImpl) itG.next(); 
			String valG = groupData.get(keyG); 
	  	 	Enumeration<Principal> roles = (Enumeration<Principal>) RequestAuthorizationImpl.getRoleGroup(valG).members();
		    while (roles.hasMoreElements())
		    {
		    	Principal rolePrincipal = roles.nextElement();
		        AttributeType attSubjectID = RequestAttributeFactory.createStringAttributeType(
		        			keyG.attribute, "tecnalia.com", "non-functional-group");
		        subject.getAttribute().add(attSubjectID);
		    }*/

			requestType.getSubject().add(subject);
		}		
	}

	/*@Override
	public void setGroupData(HashMap<SubjectAttrType, String> groupData){
		Iterator it = groupData.keySet().iterator(); 
		while(it.hasNext()) 
		{ 
			SubjectAttrType key = (SubjectAttrType) it.next(); 
			String groupString = groupData.get(key); 
			   Group group = RequestAuthorization.getRoleGroup(groupString);
			   //Create a subject type
		  	   Enumeration<Principal> roles = (Enumeration<Principal>) group.members();
		  	   while (roles.hasMoreElements())
		  	   {
		  		   Principal rolePrincipal = roles.nextElement();
		  		   AttributeType attSubjectID = RequestAttributeFactory.createStringAttributeType(
		  				 key.name(), "tecnalia.com", rolePrincipal.getName());
		  		   .getAttribute().add(attSubjectID);
		  	   }
		}
	}*/
	
	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#setService(java.util.HashMap)
	 */
	@Override
	public void setService(HashMap<String, ResourceAttrTypeImpl> resourceData) {
		ResourceType resourceType = new ResourceType();

		Iterator<String> it = resourceData.keySet().iterator(); 
		while(it.hasNext()) 
		{ 
			String key = it.next(); 
			ResourceAttrTypeImpl resourceString = resourceData.get(key); 
			URI url = null;
			try {
				url = new URI(key);
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    //Create a Resource Type
			resourceType.getAttribute().add(
					RequestAttributeFactory.createAnyURIAttributeType(
							resourceString.attribute, null,url ));
		}
		requestType.getResource().add(resourceType);
	}

	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#setAction(java.util.HashMap)
	 */
	@Override
	public void setAction(HashMap<ActionAttrTypeImpl, String> actionData) {		
		
		Iterator<ActionAttrTypeImpl> it = actionData.keySet().iterator();
		int i = 1; 
		while(it.hasNext()) 
		{ 
			if( i > 1){
				System.out.println("More than one action not permitted!!!!");
				break;
			}
			ActionAttrTypeImpl key = it.next(); 
			String actionString = actionData.get(key);
			ActionType actionType = new ActionType();
			actionType.getAttribute().add(
					RequestAttributeFactory.createStringAttributeType(
							key.attribute, "tecnalia.com", actionString));
			requestType.setAction(actionType);
		}

		
	}

	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#getSubject()
	 */
	@Override
	public HashMap<SubjectAttrTypeImpl, String> getSubject() {
		// TODO Auto-generated method stub
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#getGroupData()
	 */
	@Override
	public HashMap<SubjectAttrTypeImpl, String> getGroupData() {
		// TODO Auto-generated method stub
		return null;
	}	

	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#getService()
	 */
	@Override
	public HashMap<String, ResourceAttrTypeImpl> getService() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#getAction()
	 */
	@Override
	public HashMap<ActionAttrTypeImpl, String> getAction() {
		// TODO Auto-generated method stub
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#getRequestType()
	 */
	@Override
	public RequestType getRequestType() {
		return this.requestType;
	}	

	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.model.IRequestAuthorization#getRequestCtx()
	 */
	@Override
	public RequestCtx getRequestCtx() {
		// TODO Auto-generated method stub
		return null;
	}

    
}
